﻿// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System;

namespace CppAst
{
    /// <summary>
    /// Base class for all Cpp elements of the AST nodes.
    /// </summary>
    public abstract class CppElement : ICppElement
    {
        /// <summary>
        /// Gets or sets the source span of this element.
        /// </summary>
        public CppSourceSpan Span;

        /// <summary>
        /// Gets or sets the parent container of this element. Might be null.
        /// </summary>
        ///
        public ICppContainer Parent { get; internal set; }

        public string FullParentName
        {
            get
            {
                string tmpname = "";
                var p = Parent;
                if (p is CppClass cpp)
                {
                    return cpp.FullName;
                }

                if (p is CppNamespace ns)
                {
                    return ns.FullName;
                }

                if (p is CppCompilation)
                {
                    return "";
                }

                if (p is CppGlobalDeclarationContainer)
                {
                    return "";
                }

                return tmpname;
            }
        }

        /// <summary>
        /// Gets the source file of this element.
        /// </summary>
        public virtual string SourceFile
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(Span.Start.File))
                {
                    return Span.Start.File;
                }

                if (Parent is CppElement cppElement)
                {
                    return cppElement.SourceFile;
                }

                return Span.End.File;
            }
        }
    }
}